from abc import ABC, abstractmethod
from datetime import datetime
from typing import Dict, List
import pandas as pd

class BaseStrategy(ABC):
    """策略基类"""
    
    def __init__(self, params: Dict):
        self.params = params
        self.position = {}  # 当前持仓 {symbol: shares}
        self.capital = 1e6  # 初始资金
        
    @abstractmethod
    async def generate_signal(self, data: pd.DataFrame) -> pd.DataFrame:
        """生成交易信号
        Returns:
            DataFrame with columns: ['symbol', 'datetime', 'signal', 'strength']
        """
        pass
    
    @abstractmethod
    def on_bar(self, bar_data: Dict):
        """实时行情处理"""
        pass
    
    @abstractmethod
    def get_parameters(self) -> Dict:
        """获取策略参数"""
        pass
    
    @abstractmethod
    def version(self) -> str:
        """策略版本标识"""
        pass

class BacktestResult:
    """回测结果封装类"""
    def __init__(self):
        self.returns = []       # 每日收益率
        self.positions = {}     # 每日持仓
        self.trades = []         # 交易记录
        self.metrics = {         # 绩效指标
            'sharpe': 0.0,
            'max_drawdown': 0.0,
            'annual_return': 0.0
        }